import React, { Component } from 'react';
import { StyleSheet, WebView, View, Text } from 'react-native';
import { SafeAreaView } from 'react-navigation';
import PropTypes from 'prop-types';
import ViewUtil from '../utils/View';
import BaseStyle from '../constants/Style';

export default class Web extends Component {
  static propTypes = {
    navigation: PropTypes.object,
  };
  constructor(props) {
    super(props);
    this.state = {
      error: false,
    };
  }
  componentDidMount() {}
  componentWillUnmount() {}

  render() {
    const { navigation } = this.props;
    const uri = navigation.getParam('uri');

    return (
      <SafeAreaView style={BaseStyle.flex}>
        {!this.state.error ? (
          <WebView
            source={{ uri: uri }}
            startInLoadingState={true}
            // javaScriptEnabled={true}
            // mixedContentMode={'always'}
            // dataDetectorTypes={'all'}
            ref={webView => (this.webView = webView)}
            scalesPageToFit={true}
            scrollEnabled={true}
            onError={() => this.setState({ error: true })}
            style={{ width: ViewUtil.size.width, height: ViewUtil.size.height }}
          />
        ) : (
          <View style={styles.view}>
            <Text>页面不见了...</Text>
          </View>
        )}
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  view: {
    ...BaseStyle.flex,
    ...BaseStyle.alignItemsCenter,
    ...BaseStyle.justifyContentCenter,
  },
});
